<template>
  <view id="selectBox" :style="assemblyStyle">
    <view @tap="showOptions" class="selectBox">
      <input
        @input="updateValue"
        v-model="checked"
        :placeholder="peaseSelect"
        disabled
        placeholder-style="
      color: #ccccd3;"
        class="peaseSelect fs_16" />
      <uni-icons
        v-if="!checked"
        class="icon"
        :type="type"
        color="#ccccd3"
        size="16" />
      <view v-else class="icon" @tap.stop="clearSelect">
        <uni-icons type="close" color="#ccccd3" size="18" />
      </view>
    </view>
    <view v-if="showoptionsList" class="optionsList">
      <view v-for="(option, index) in options" :key="index" class="option">
        <view class="optionContent fs_16" @click="selectOption(option)">
          {{ option[label] }}
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
const prop = defineProps({
  peaseSelect: {
    type: String,
    default: "请选择",
  },
  options: {
    type: Array,
    default: () => [],
  },
  value: {
    type: String,
    default: "",
  },
  label: {
    type: String,
    default: "",
  },
  assemblyStyle: {
    type: String,
    default: "",
  },
});
// 箭头方向
const type = ref("down");
// 选项值
const value = ref("");
// 选中的选项
const checked = ref("");
const emit = defineEmits(["selectOption", "update:modelValue"]);
// 显示选项列表
const showoptionsList: any = ref(false);
const showOptions = () => {
  type.value = type.value === "down" ? "up" : "down";
  showoptionsList.value = type.value === "down" ? false : true;
};
// 清空选项
const clearSelect = () => {
  // 清空选项
  checked.value = "";
  value.value = "";
  showoptionsList.value = false;
  type.value = "down";
  // 触发选择事件
  emit("selectOption", "");
  emit("update:modelValue", value.value);
};
// 更新选项值
const updateValue = (event: any) => {
  nextTick(() => {
    value.value = event.target.value;
    console.log("update:modelValue", event);
    emit("update:modelValue", event.target.value);
  });
};
const selectOption = (option: any) => {
  // 选择选项
  checked.value = option[prop.label];
  // 选项值
  value.value = option[prop.value];
  // 关闭选项列表
  showoptionsList.value = false;
  type.value = "down";
  // 触发选择事件
  emit("update:modelValue", value.value);
  emit("selectOption", option[prop.value]);
};
</script>

<style lang="scss" scoped>
#selectBox {
  position: relative;
  width: 120px;
  .selectBox {
    height: 40px;
    text-align: center;
    line-height: 40px;
    padding: 0 5px;
    border: 1px solid rgb(228, 228, 228);
    border-radius: 6px;
    background: rgb(255, 255, 255);
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    .peaseSelect {
      width: 100%;
    }
    .icon {
      margin-left: 10px;
    }
  }
  .optionsList {
    width: 100%;
    position: absolute;
    z-index: 1001;
    border: 1px solid #e4e7ed;
    border-radius: 4px;
    background-color: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
    margin: 5px 0;
    padding: 5px 0;
    .option {
      padding: 0 20px;
      margin: 5px 0;
    }
  }
}
</style>
